$.extend($.fn.panel.defaults, {
    //默认加入刷新图标
    tools: [{
    	iconCls: "icon-mini-refresh", 
    	handler:function(array,selector){
    		$("#mainTabs").tabs("refresh") ;
    	}
    }]
});

$.extend($.fn.tabs.defaults, {
	onSelect: function(title, index) {
		var tabs = $(this) ;
		$.util.exec(function () {
			var currentTab = tabs.tabs('getSelected'), panelOpts = currentTab.panel("options");
			
			if(undefined == panelOpts.location) {
				window.location.hash = '';
			} else {
				window.location.hash = panelOpts.location;
			}
		});
	},
	onClose: function(title, index) {
		var tabs = $(this) ;
		$.util.exec(function () {
			var currentTab = tabs.tabs('getSelected'), panelOpts = currentTab.panel("options");
			
			if(undefined == panelOpts.location) {
				window.location.hash = '';
			} else {
				window.location.hash = panelOpts.location;
			}
	    });
	}
})


$.extend($.fn.tabs.methods, {
	allTabs: function(target) {
		var tabs = $(target).tabs('tabs');
		var all = [];
		all = $.map(tabs, function(n, i) {
			return $(n).panel('options')
		});
		return all;
	},
	refresh: function (jq) { //刷新
		var tabs = $(jq), currentTab = tabs.tabs("getSelected"), opts = tabs.tabs("options"), panelOpts = currentTab.panel("options");
		if ($.string.isNullOrWhiteSpace(panelOpts.href) && $.string.isNullOrWhiteSpace(panelOpts.location)) { return ; }
	    if ($.string.isNullOrWhiteSpace(panelOpts.location) && $.string.isNullOrWhiteSpace(panelOpts.content)) {
	    	//panel刷新
	    	currentTab.panel('refresh');
	    } else { 
	    	//iframe 刷新
	    	tabs.tabs("update", { tab: currentTab, options: panelOpts });
	    }
	},
	closeCurrent : function(target) { // 关闭当前
		var currentTab = $(target).tabs('getSelected'), currentTabIndex = $(target).tabs('getTabIndex', currentTab);
		$(target).tabs('close', (currentTabIndex>0?currentTabIndex:null));
	},
	closeAll : function(target, homePageTitle) { // 关闭全部
		var tabs = $(target).tabs('allTabs');
		$.each(tabs, function(i, n) {
			$(target).tabs('close', n.title!=homePageTitle?n.title:null);
		})
	},
	closeOther : function(target, homePageTitle) { // 关闭除当前标签页外的tab页
		var tabs = $(target).tabs('allTabs');
		var currentTab = $(target).tabs('getSelected'), currentTabIndex = $(target)
				.tabs('getTabIndex', currentTab);

		$.each(tabs, function(i, n) {
			if (currentTabIndex != i) {
				$(target).tabs('close', n.title!=homePageTitle?n.title:null);
			}
		})
	},
	closeLeft : function(target, homePageTitle) { // 关闭当前页左侧tab页
		var tabs = $(target).tabs('allTabs');
		var currentTab = $(target).tabs('getSelected'), currentTabIndex = $(target)
				.tabs('getTabIndex', currentTab);
		var i = currentTabIndex - 1;

		while (i > -1) {
			$(target).tabs('close', tabs[i].title!=homePageTitle?tabs[i].title:null);
			i--;
		}
	},
	closeRight : function(target, homePageTitle) { // // 关闭当前页右侧tab页
		var tabs = $(target).tabs('allTabs');
		var currentTab = $(target).tabs('getSelected'), currentTabIndex = $(target)
				.tabs('getTabIndex', currentTab);
		var i = currentTabIndex + 1, len = tabs.length;
		while (i < len) {
			$(target).tabs('close', tabs[i].title!=homePageTitle?tabs[i].title:null);
			i++;
		}
	},
	jumpTab : function(target) {
		var t = $(target), which = $(target).tabs('getSelected').panel('options').title,
		    tab = (which == null || which == undefined) ? t.tabs("getSelected") : t.tabs("getTab", which),
		    opts = tab.panel("options");
		if (opts.iniframe == 2) {
		    window.open(opts.location, "_blank");
		} else {
			$.messager.alert("提示信息", "\"" + opts.title + "\" 选项卡不可在新页面中打开。",'info');
		}
	}
});


